//
//  CAAddressPickerView.m
//  LNH
//
//  Created by twentylx on 2018/2/6.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAAddressPickerView.h"
#import "CACityModel.h"

@interface CAAddressPickerView() <UIPickerViewDelegate, UIPickerViewDataSource>

//@property (nonatomic, strong) NSDictionary *countriesDict;//存储国家的信息
@property (nonatomic, strong) NSArray *provinceList;//存储所有的省的名称
@property (nonatomic, strong) NSDictionary *regionDict;//存储所有的地区的名称
@property (nonatomic, strong) NSDictionary *cityDict;//存储所有的城市的名称

@property (nonatomic, strong) NSArray *selectCityList;//选中的城市
@property (nonatomic, strong) NSArray *selectRegionList;//选中的地区

@property (nonatomic, strong) CACityModel *cityModel;

@end

@implementation CAAddressPickerView


-(id) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(!self) return nil;
    
    [self setBackgroundColor:[UIColor whiteColor]];
    
    //设置代理
    self.delegate = self;
    self.dataSource = self;
    
//    [self getPickerData];
    
    return self;
}

//得到数据
- (void) getPickerData {
    
//    NSString *path =  [[NSBundle mainBundle] pathForResource:@"location" ofType:@"json"];
//    NSData *data = [NSData dataWithContentsOfFile:path];
//
//    self.countriesDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    self.provinceList  = self.localArray;
//    self.cityDict      = self.provinceList[@"city"];
//    self.regionDict    = self.localDict[@"areaList"];
    
    //首次加载默认城市
    self.selectCityList = self.provinceList[0][@"city"];
    //首次加载默认地区
    self.selectRegionList = self.selectCityList[0][@"area"];
}

-(NSArray *) getRegionKey:(NSString *) province city:(NSString *) city {
    NSString *key = [NSString stringWithFormat:@"%@-%@", province, city];
    NSArray *list = self.regionDict[key];
    return list;
}

-(RACSubject *) selecedCitySubject {
    if (!_selecedCitySubject) {
        _selecedCitySubject = [[RACSubject alloc] init];
    }
    return _selecedCitySubject;
}

-(CACityModel *) cityModel {
    if (!_cityModel) {
        _cityModel = [[CACityModel alloc] init];
    }
    return _cityModel;
}

#pragma mark --- UIPickerViewDataSource代理方法
//设置列的返回数量
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

//设置列里边组件的个数 component:组件
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (component==0) {
        return self.provinceList.count;
    } else if (component == 1){
        return self.selectCityList.count;
    } else{
        return self.selectRegionList.count;
    }
    
}


#pragma mark --- UIPickerViewDelegate代理方法
//返回组件的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    if (component == 0) {
        return [self.provinceList objectAtIndex:row][@"name"];
    } else if (component == 1) {
        return [self.selectCityList objectAtIndex:row][@"name"];
    } else {
        return [self.selectRegionList objectAtIndex:row][@"name"];
    }
    
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component == 0) {
        
        self.cityModel.province = self.provinceList[row][@"name"];
        self.cityModel.provinceId = self.provinceList[row][@"id"];
        
        self.selectCityList = self.provinceList[row][@"city"];
        self.cityModel.city = self.selectCityList[0][@"name"];
        self.cityModel.cityId = self.selectCityList[0][@"id"];
        
        self.selectRegionList = self.selectCityList[0][@"area"];
        self.cityModel.region = self.selectRegionList[0][@"name"];
        self.cityModel.regionId = self.selectRegionList[0][@"id"];
        
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
        
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView selectRow:0 inComponent:2 animated:YES];
        
        [self.selecedCitySubject sendNext:self.cityModel];
        
    } else if (component == 1) {
        
        if (self.cityModel.province == nil) {
            
            self.cityModel.province = @"北京市";
            self.cityModel.provinceId = @"1";
        }
        
        self.cityModel.city = self.selectCityList[row][@"name"];
        self.cityModel.cityId = self.selectCityList[row][@"id"];
        
        self.selectRegionList = self.selectCityList[row][@"area"];
        self.cityModel.region = self.selectRegionList[0][@"name"];
        self.cityModel.regionId = self.selectRegionList[0][@"id"];
        
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
        
        [self.selecedCitySubject sendNext:self.cityModel];
        
    } else if (component == 2 ) {
        
        if (self.cityModel.province == nil) {
            
            self.cityModel.province = @"北京市";
            self.cityModel.provinceId = @"1";
        }
        if (self.cityModel.city == nil) {
            
            self.cityModel.city = @"北京市";
            self.cityModel.cityId = @"1";
        }
        self.cityModel.region = self.selectRegionList[row][@"name"];
        self.cityModel.regionId = self.selectRegionList[row][@"id"];
        
        [self.selecedCitySubject sendNext:self.cityModel];
    }
}

@end
